package com.itttq.gshop.admin.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.po.OrderInfoPo;
import com.itttq.gshop.domain.po.UserInfoPo;
import com.itttq.gshop.domain.po.WareInfoPo;
import com.itttq.gshop.domain.to.IndexStatTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.cmpt.annotation.Auth;
import com.itttq.gshop.generic.connector.IOrderInfoConnector;
import com.itttq.gshop.generic.connector.IUserInfoConnector;
import com.itttq.gshop.generic.connector.IWareInfoConnector;
import com.itttq.gshop.generic.util.Constants;
@Auth(excls={"login"})
@Controller
public class IndexController extends GenericController {
	private static final long serialVersionUID = 2189470950931490968L;
	@Autowired
	private IUserInfoConnector userInfoConnector;
	@Autowired
	private IOrderInfoConnector orderInfoConnector;
	@Autowired
	private IWareInfoConnector wareInfoConnector;
	
	// 记录统计的Map。
	public static Map<String, String> statMap = new HashMap<String, String>();
	@Auth
	@RequestMapping(value={"index.html"})
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("index");
		// 此处来初始化
		int pageSize = 5;
		// TODO初始化
		OrderInfoPo order = new OrderInfoPo();
		order.setSize(pageSize);
		ResultPageTo<OrderInfoPo> orders = orderInfoConnector.getOrderInfos(order);
		statMap.put("orders", String.valueOf(orders.getTotal()));
		mv.addObject("orders", orders.getData());
		IndexStatTo ist = orderInfoConnector.getOrderStat();
		BigDecimal amount = ist.getTotal();
		statMap.put("amounts", null == amount ? "0" : String.valueOf(amount.intValue()));
		statMap.put("ordersToday", String.valueOf(ist.getCountToday()));
		amount = ist.getTotalToday();
		statMap.put("amountsToday", null == amount ? "0" : String.valueOf(amount.intValue()));
		
		UserInfoPo user = new UserInfoPo();
		user.setSize(pageSize);
		ResultPageTo<UserInfoPo> users = userInfoConnector.getUserInfos(user);
		statMap.put("users", String.valueOf(users.getTotal()));
		mv.addObject("users", users.getData());
		ist = userInfoConnector.getUserCountToday();
		mv.addObject("usersToday", ist.getCountToday());
		
		WareInfoPo ware = new WareInfoPo();
		ware.setSize(pageSize);
		ResultPageTo<WareInfoPo> wares = wareInfoConnector.getWareInfos(ware);
		statMap.put("wares", String.valueOf(orders.getTotal()));
		mv.addObject("wares", wares.getData());
		ist = wareInfoConnector.getWareCountToday();
		mv.addObject("waresToday", ist.getCountToday());
		mv.addObject("stat", statMap);
		return mv;
	}
	
	@RequestMapping(value={"login.html", ""})
	public ModelAndView login() {
		return new ModelAndView("login");
	}
	
	/**
	 * 跳转到顶部页面
	 * @param request
	 * @return
	 */
	@RequestMapping("top.html")
	public ModelAndView topPage(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("top");
		mv.addObject("user", this.getSession(request, Constants.SESSION_USER));
		return mv;
	}
	
	/**
	 * 跳转到根目录静态页面的导航页面
	 * @return
	 */
	@Auth
	@RequestMapping("{page}.html")
	public ModelAndView gotoRootPage(@PathVariable("page") String page) {
		return new ModelAndView(page);
	}

}
